package com.linc.readdata;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class MainActivity extends Activity {
	TextView txt = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txt = (TextView)findViewById(R.id.txt_show);
		txt.setMovementMethod(ScrollingMovementMethod.getInstance());
		modifyExcel();
//		writeExcel();
		readExcel();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void readExcel() {
		  try {
		   InputStream is = new FileInputStream("mnt/sdcard/test.xls");
		   //Workbook book = Workbook.getWorkbook(new File("mnt/sdcard/test.xls"));
		   Workbook book = Workbook.getWorkbook(is);
		   int num = book.getNumberOfSheets();
		   txt.setText("the num of sheets is " + num+ "\n");
		   // 获得第一个工作表对象
		   Sheet sheet = book.getSheet(0);
		   int Rows = sheet.getRows();
		   int Cols = sheet.getColumns();
		   txt.append("the name of sheet is " + sheet.getName() + "\n");
		   txt.append("total rows is " + Rows + "\n");
		   txt.append("total cols is " + Cols + "\n");
		   Rows = 3;
		   Cols = 8;
		   for (int i = 0; i < Cols; ++i) {
		    for (int j = 0; j < Rows; ++j) {
		     // getCell(Col,Row)获得单元格的值
		     	txt.append("contents:" + sheet.getCell(i,j).getContents() + "\n");
		    }
		   }
		   book.close();
		  } catch (Exception e) {
			  txt.setText("error: "+e.getMessage());
		   System.out.println(e);
		  }
		}
	
	private void writeExcel() {

		try {
			WritableWorkbook workbook = Workbook.createWorkbook(new File("mnt/sdcard/test2.xls"));
//			WritableSheet sheet2 = workbook.getSheet(0);
			WritableSheet sheet2 = workbook.createSheet("First Sheet", 0); 
			WritableCell cell = sheet2.getWritableCell(1, 2);

			if (cell.getType() == CellType.LABEL)
			{
			  Label l = (Label) cell;
			  l.setString("modified cell---linc");
			} 
			
			Label label = new Label(0, 2, "New label record---linc");
			sheet2.addCell(label);

			Number number = new Number(3, 4, 3.1459);
			sheet2.addCell(number); 
			
			workbook.write();
			workbook.close(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	private void modifyExcel() {
		try {
		    InputStream is = new FileInputStream("mnt/sdcard/test.xls");
		    Workbook book = Workbook.getWorkbook(is);
			WritableWorkbook workbook = Workbook.createWorkbook(new File("mnt/sdcard/test.xls"),book);
			
			WritableSheet sheet2 = workbook.getSheet(0);
			WritableCell cell = sheet2.getWritableCell(1, 2);

			if (cell.getType() == CellType.LABEL)
			{
			  Label l = (Label) cell;
			  l.setString("modified cell");
			} 
			
			Label label = new Label(0, 2, "New label record");
			sheet2.addCell(label);

			Number number = new Number(3, 4, 3.1459);
			sheet2.addCell(number); 
			
			workbook.write();
			workbook.close(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
